package com.dwjm.exam.stu_reg;

import com.dwjm.exam.pojo.Student;
import com.dwjm.utils.CommonResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@WebServlet("/reg")
public class RegHandler extends HttpServlet {
    private RegDao regDao = new RegDaoImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        // 获取请求参数
        String studentId = req.getParameter("studentId");
        String name = req.getParameter("name");
        String gender = req.getParameter("gender");
        String birthDateStr = req.getParameter("birthDate");
        String idCard = req.getParameter("idCard");
        String college = req.getParameter("college");
        String major = req.getParameter("major");
        String className = req.getParameter("className");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        // 验证必填字段
        if (studentId == null || studentId.trim().isEmpty() ||
                name == null || name.trim().isEmpty() ||
                username == null || username.trim().isEmpty() ||
                password == null || password.trim().isEmpty()) {
            resp.getWriter().write("N");
            return;
        }

        // 创建Student对象
        Student student = new Student();
        student.setStudentId(studentId);
        student.setName(name);
        student.setGender(gender);

        try {
            if (birthDateStr != null && !birthDateStr.trim().isEmpty()) {
                student.setBirthDate(new SimpleDateFormat("yyyy-MM-dd").parse(birthDateStr));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }

        student.setIdCard(idCard);
        student.setCollege(college);
        student.setMajor(major);
        student.setClassName(className);
        student.setStatus("在读"); // 默认状态为"在读"
        student.setPhone(phone);
        student.setEmail(email);
        student.setUsername(username);
        student.setPassword(password);

        // 检查学号是否已存在
        if (regDao.isStudentIdExists(studentId)) {
            resp.getWriter().write("EID"); // 学号已存在
            return;
        }

        // 检查用户名是否已存在
        if (regDao.isUsernameExists(username)) {
            resp.getWriter().write("EUSER"); // 用户名已存在
            return;
        }

        // 执行注册
        boolean success = regDao.registerStudent(student);
        if (success) {
            resp.getWriter().write("Y");
        } else {
            resp.getWriter().write("N");
        }
    }
}